﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebChat.Admin
{
    public partial class ConfirmDelete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ButtonDelete.Visible = false;
            string chatterIdString = Request["chatterId"];

            if (chatterIdString == null)
            {
                this.LabelInfo.Text = "Invalid chatterId.";
                return;
            }

            int chatterId;
            if (!Int32.TryParse(chatterIdString, out chatterId))
            {
                this.LabelInfo.Text = "Invalid chatterId.";
                return;
            }

            WebChatDAL.WebChatDAO dao = new WebChatDAL.WebChatDAO();

            WebChatDAL.Chatter chatter;
            if (!dao.TryGetChatterByChatterId(chatterId, out chatter))
            {
                this.LabelInfo.Text = String.Format( "Chatter with this chatterId({0}) does not exist.", chatterId);
                return;
            }
            
            this.LabelInfo.Text="Are you sure you want to delete " + chatter.aspnet_Users.UserName;
            this.ButtonDelete.Visible = true;
        }

        protected void ButtonDelete_OnClick(object sender, EventArgs args)
        {
            string chatterIdString = Request["chatterId"];

            if (chatterIdString == null)
            {
                this.LabelInfo.Text = "Invalid chatterId.";
                return;
            }

            int chatterId;
            if (!Int32.TryParse(chatterIdString, out chatterId))
            {
                this.LabelInfo.Text = "Invalid chatterId.";
                return;
            }

            WebChatDAL.WebChatDAO dao = new WebChatDAL.WebChatDAO();

            WebChatDAL.Chatter chatter;
            if (!dao.TryGetChatterByChatterId(chatterId, out chatter))
            {
                this.LabelInfo.Text = String.Format("Chatter with this chatterId({0}) does not exist.", chatterId);
                return;
            }

            dao.DeleteChatterByChatterId(chatter.ChatterId);

            Response.Redirect("Chatters.aspx");
        }
    }
}